#-------------------------------------------------------------------------------
# Name:        savedialogs
# Purpose:
#
# Author:      Pablo
#
# Created:     05/09/2013
# Copyright:   (c) Monxcleyr Productions 2013
# Licence:     GPL v3
#-------------------------------------------------------------------------------
#!/usr/bin/env python

import pygame, os
import label, textbox

class nameDialog():

  """Brings up a dialog to set the name of a new game save"""

  def __init__(self, surface, dialogimage, pos, namefont, detailsfont, fontcolor, clicksound, hoversound, common, sound, settings, graphics):
    self.menuSurface = surface
    self.image = pygame.image.load(os.path.join('resources/graphics/' + dialogimage)).convert_alpha()
    self.pos = (pos[0] - self.image.get_width() / 2, pos[1] - self.image.get_height() / 2)
    self.namefont = namefont
    self.detailsfont = detailsfont
    self.fontcolor = fontcolor
    self.clicksound = clicksound
    self.hoversound = hoversound
    self.common = common
    self.accept = graphics[0]
    self.accepthover = graphics[1]
    self.cancel = graphics[2]
    self.cancelhover = graphics[3]
    self.sound = sound
    self.settings = settings
    self.firstdraw = True

    pos = (self.pos[0] + self.image.get_width() / 2, self.pos[1] + self.namefont.size('Name this save')[1] + 10)
    self.label = label.Label(self.menuSurface, 'Name this save:', pos, 'center', self.namefont, self.fontcolor)


    pos = (self.pos[0] + self.image.get_width() / 2 - 200 / 2, self.pos[1]  + 80)

    self.namebox = textbox.TextBox(self.menuSurface, self.detailsfont, 'New Game', 200, pos, self.fontcolor)


    self.menu_item = []
    self.acceptpos = (self.pos[0] + self.image.get_width() / 2 - 200 / 2, self.pos[1]  + 125)
    self.cancelpos = (self.pos[0] + self.image.get_width() / 2 + 200 / 2, self.pos[1]  + 125)

    self.menu_item.append(MenuItem(1, self.acceptpos, self.accept, self.accepthover, self.clicksound, self.hoversound, 1, ' '))
    self.menu_item.append(MenuItem(2, self.cancelpos, self.cancel, self.cancelhover, self.clicksound, self.hoversound, 2, ' '))



  def draw(self, mouse):

    if self.firstdraw:
      mouse[1] = 0
      self.firstdraw = False

    self.mouse = mouse

    self.menuSurface.blit(self.image, self.pos)
    self.label.draw()

    key = 0
    for number in range(32,127):
      if self.common.keyState.check(number)[0]:
        key = number

    if self.common.keyState.check(pygame.K_BACKSPACE)[0]:
      key = pygame.K_BACKSPACE

    self.namebox.draw(mouse[0], mouse[1], key)

    # Check buttons to draw, hovered or normal:
    hover = self.getHover() # find a seclected button, if any
    for mitem in self.menu_item: #Get each menu item from the list
      if hover == mitem.id:
        self.menuSurface.blit(mitem.buttonhover, mitem.pos)
      else:
        self.menuSurface.blit(mitem.button, mitem.pos)

    self.selected, self.mode = self.getSelected()



  def reset(self):

    self.namebox.setText('New Game')
    self.firstdrawd = True
    self.selected = 0



#-------------------------------------------------------------------------------

  def getHover(self):

    """ Returns hovered option. 0 if none."""

    selected = 0

    for i in range(0, len(self.menu_item)):
      if self.menu_item[i].rect.collidepoint(self.mouse[0]):
        if not self.menu_item[i].prevhover:
          self.menu_item[i].prevhover = True
          self.sound.play(self.menu_item[i].hoversound, self.settings)
        selected = self.menu_item[i].id
      else:
        self.menu_item[i].prevhover = False

    return selected


  def getSelected(self):

    """ Returns selected option. 0 if none."""

    selected = 0
    mode = 0

    if self.mouse[1] == 1:
      pressed = True
    else:
      pressed = False
    for i in range(0, len(self.menu_item)):
      if self.menu_item[i].rect.collidepoint(self.mouse[0]) and pressed == True:
        self.sound.play(self.menu_item[i].sound, self.settings)
        selected = self.menu_item[i].id
        mode = self.menu_item[i].mode

    return selected, mode


#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------


class overwriteDialog():

  """Brings up a dialog that confirms if they want to overwrite their save"""

  def __init__(self, surface, dialogimage, pos, namefont, detailsfont, fontcolor, clicksound, hoversound, common, sound, settings, graphics):
    self.menuSurface = surface
    self.image = pygame.image.load(os.path.join('resources/graphics/' + dialogimage)).convert_alpha()
    self.pos = (pos[0] - self.image.get_width() / 2, pos[1] - self.image.get_height() / 2)
    self.namefont = namefont
    self.detailsfont = detailsfont
    self.fontcolor = fontcolor
    self.clicksound = clicksound
    self.hoversound = hoversound
    self.common = common
    self.accept = graphics[0]
    self.accepthover = graphics[1]
    self.cancel = graphics[2]
    self.cancelhover = graphics[3]
    self.sound = sound
    self.settings = settings
    self.firstdraw = True

    pos = (self.pos[0] + self.image.get_width() / 2, self.pos[1] + self.namefont.size('Name this save')[1] + 20)
    self.label = label.Label(self.menuSurface, 'Overwrite existing save?', pos, 'center', self.namefont, self.fontcolor)


    self.menu_item = []
    self.acceptpos = (self.pos[0] + self.image.get_width() / 2 - 200 / 2, self.pos[1]  + 125)
    self.cancelpos = (self.pos[0] + self.image.get_width() / 2 + 200 / 2, self.pos[1]  + 125)

    self.menu_item.append(MenuItem(1, self.acceptpos, self.accept, self.accepthover, self.clicksound, self.hoversound, 1, ' '))
    self.menu_item.append(MenuItem(2, self.cancelpos, self.cancel, self.cancelhover, self.clicksound, self.hoversound, 2, ' '))



  def draw(self, mouse):

    if self.firstdraw:
      mouse[1] = 0
      self.firstdraw = False

    self.mouse = mouse

    self.menuSurface.blit(self.image, self.pos)
    self.label.draw()

    key = 0
    for number in range(32,127):
      if self.common.keyState.check(number)[0]:
        key = number

    if self.common.keyState.check(pygame.K_BACKSPACE)[0]:
      key = pygame.K_BACKSPACE

    # Check buttons to draw, hovered or normal:
    hover = self.getHover() # find a seclected button, if any
    for mitem in self.menu_item: #Get each menu item from the list
      if hover == mitem.id:
        self.menuSurface.blit(mitem.buttonhover, mitem.pos)
      else:
        self.menuSurface.blit(mitem.button, mitem.pos)

    self.selected, self.mode = self.getSelected()



  def reset(self):

    self.firstdrawd = True
    self.selected = 0



#-------------------------------------------------------------------------------

  def getHover(self):

    """ Returns hovered option. 0 if none."""

    selected = 0

    for i in range(0, len(self.menu_item)):
      if self.menu_item[i].rect.collidepoint(self.mouse[0]):
        if not self.menu_item[i].prevhover:
          self.menu_item[i].prevhover = True
          self.sound.play(self.menu_item[i].hoversound, self.settings)
        selected = self.menu_item[i].id
      else:
        self.menu_item[i].prevhover = False

    return selected


  def getSelected(self):

    """ Returns selected option. 0 if none."""

    selected = 0
    mode = 0

    if self.mouse[1] == 1:
      pressed = True
    else:
      pressed = False
    for i in range(0, len(self.menu_item)):
      if self.menu_item[i].rect.collidepoint(self.mouse[0]) and pressed == True:
        self.sound.play(self.menu_item[i].sound, self.settings)
        selected = self.menu_item[i].id
        mode = self.menu_item[i].mode

    return selected, mode



#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------


class confirmQuitDialog():

  """Brings up a dialog that confirms if they want to overwrite their save"""

  def __init__(self, surface, dialogimage, pos, namefont, detailsfont, fontcolor, clicksound, hoversound, common, sound, settings, graphics):
    self.menuSurface = surface
    self.image = pygame.image.load(os.path.join('resources/graphics/' + dialogimage)).convert_alpha()
    self.pos = (pos[0] - self.image.get_width() / 2, pos[1] - self.image.get_height() / 2)
    self.namefont = namefont
    self.detailsfont = detailsfont
    self.fontcolor = fontcolor
    self.clicksound = clicksound
    self.hoversound = hoversound
    self.common = common
    self.accept = graphics[0]
    self.accepthover = graphics[1]
    self.cancel = graphics[2]
    self.cancelhover = graphics[3]
    self.sound = sound
    self.settings = settings
    self.firstdraw = True

    pos = (self.pos[0] + self.image.get_width() / 2, self.pos[1] + self.namefont.size('Name this save')[1] + 10)
    self.label = label.Label(self.menuSurface, 'Return to Menu?', pos, 'center', self.namefont, self.fontcolor)
    pos = (pos[0], pos[1] + self.namefont.size('Name this save')[1] + 3)
    self.label2 = label.Label(self.menuSurface, 'Unsaved changes will be lost.', pos, 'center', self.detailsfont, self.fontcolor)


    self.menu_item = []
    self.acceptpos = (self.pos[0] + self.image.get_width() / 2 - 200 / 2, self.pos[1]  + 125)
    self.cancelpos = (self.pos[0] + self.image.get_width() / 2 + 200 / 2, self.pos[1]  + 125)

    self.menu_item.append(MenuItem(1, self.acceptpos, self.accept, self.accepthover, self.clicksound, self.hoversound, 1, ' '))
    self.menu_item.append(MenuItem(2, self.cancelpos, self.cancel, self.cancelhover, self.clicksound, self.hoversound, 2, ' '))



  def draw(self, mouse):

    if self.firstdraw:
      mouse[1] = 0
      self.firstdraw = False

    self.mouse = mouse

    self.menuSurface.blit(self.image, self.pos)
    self.label.draw()
    self.label2.draw()

    key = 0
    for number in range(32,127):
      if self.common.keyState.check(number)[0]:
        key = number

    if self.common.keyState.check(pygame.K_BACKSPACE)[0]:
      key = pygame.K_BACKSPACE

    # Check buttons to draw, hovered or normal:
    hover = self.getHover() # find a seclected button, if any
    for mitem in self.menu_item: #Get each menu item from the list
      if hover == mitem.id:
        self.menuSurface.blit(mitem.buttonhover, mitem.pos)
      else:
        self.menuSurface.blit(mitem.button, mitem.pos)

    self.selected, self.mode = self.getSelected()



  def reset(self):

    self.firstdrawd = True
    self.selected = 0



#-------------------------------------------------------------------------------

  def getHover(self):

    """ Returns hovered option. 0 if none."""

    selected = 0

    for i in range(0, len(self.menu_item)):
      if self.menu_item[i].rect.collidepoint(self.mouse[0]):
        if not self.menu_item[i].prevhover:
          self.menu_item[i].prevhover = True
          self.sound.play(self.menu_item[i].hoversound, self.settings)
        selected = self.menu_item[i].id
      else:
        self.menu_item[i].prevhover = False

    return selected


  def getSelected(self):

    """ Returns selected option. 0 if none."""

    selected = 0
    mode = 0

    if self.mouse[1] == 1:
      pressed = True
    else:
      pressed = False
    for i in range(0, len(self.menu_item)):
      if self.menu_item[i].rect.collidepoint(self.mouse[0]) and pressed == True:
        self.sound.play(self.menu_item[i].sound, self.settings)
        selected = self.menu_item[i].id
        mode = self.menu_item[i].mode

    return selected, mode


#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------


class MenuItem:
  def __init__(self, id, pos, button, buttonhover, sound, hoversound, mode, link):

    self.id = id
    self.pos = pos
    self.button = pygame.image.load(os.path.join('resources/graphics/' + button)).convert_alpha()
    self.buttonhover = pygame.image.load(os.path.join('resources/graphics/' + buttonhover)).convert_alpha()
    #center image:
    self.pos = (pos[0] - (self.button.get_width() / 2), pos[1] - (self.button.get_height() / 2))
    self.rect = self.get_rect()
    self.sound = sound
    self.hoversound = hoversound
    self.mode = mode
    self.link = link
    self.prevhover = False

  def get_rect(self):
    return pygame.Rect(self.pos, (self.button.get_width(), self.button.get_height()))

